import 'dart:math';

import 'package:blue_classic_plus/BluetoothDevice.dart';
import 'package:blue_classic_plus_example/PhoneFlashUtil.dart';
import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:blue_classic_plus/blue_classic_plus.dart';
import 'package:mybase/base/tool/color/FFColor.dart';
import 'package:mybase/base/tool/empty/FFEmpty.dart';
import 'package:mybase/base/tool/extension/FFStringExt.dart';
import 'package:mybase/base/tool/globalMethod/FFSafeValue.dart';
import 'package:mybase/base/tool/globalMethod/FFScript.dart';
import 'package:mybase/base/tool/pprint/FFLog.dart';
import 'package:mybase/base/widget/layout/FFLineContainer.dart';
import 'package:mybase/base/widget/layout/FFTestClick.dart';
import 'package:mybase/base/widget/layout/FFTextButton.dart';

/**
 * 使用该插件，需要在主工程里面app目录下的proguard-rules.pro文件里面
 * 添加
 * -keep class com.example.blue_classic_plus.** { *; }
 */

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final bluePlugin = BlueClassicPlus();

  ///模拟发送的消息内容
  int msgIndex = 0;

  ///服务端是否开启
  bool serverStart = false;

  @override
  void initState() {
    super.initState();

    ///初始化一些信息
    BlueClassicPlus.delegate = BlueClassicPlusDelegate();
    BlueClassicPlus.initEvent();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('经典蓝牙开发'),
        ),
        body: SingleChildScrollView(
          child: FFLineContainer(
            direction: Axis.vertical,
            children: [
              FFTestClick(
                text: '检查蓝牙是否可用',
                onClick: () async {
                  bool isSupport = await bluePlugin.bluetoothIsSupport();
                  FFLog.result(isSupport, "检查蓝牙是否可用");
                },
              ),
              FFTestClick(
                text: '请求蓝牙权限',
                onClick: () async {
                  bool bluetoothPermission =
                      await bluePlugin.bluetoothPermission();
                  FFLog.result(bluetoothPermission, "蓝牙权限是否有了");
                },
              ),
              FFTestClick(
                text: '检查蓝牙是否打开',
                onClick: () async {
                  bool isOpen = await bluePlugin.bluetoothIsEnabled();
                  FFLog.result(isOpen, "检查蓝牙是否打开");
                },
              ),
              FFTestClick(
                text: '检查蓝牙可见性',
                onClick: () async {
                  bool isOpen = await bluePlugin.bluetoothEnableEetectability();
                  FFLog.result(isOpen, "检查蓝牙可见性");
                },
              ),
              FFTestClick(
                text: '查找系统已配对设备',
                onClick: () async {
                  List<BluetoothDevice> devices =
                      await bluePlugin.bluetoothBondedDevices();
                  FFLog.result(!FFEmpty.listIsEmpty(devices), "查找系统已配对设备");
                  for (var entity in devices) {
                    FFLog.info(
                        "查找系统已配对设备 name = ${entity.name} ,address = ${entity.address}");
                  }
                },
              ),
              FFTestClick(
                text: "打开蓝牙服务端",
                onClick: () async {
                  bool serverStartSuccess =
                      await bluePlugin.bluetoothStartServer(
                    name: "LedServer",
                    uuid: "E20A39F4-73F5-4BC4-A12F-17D1AD07A961",
                  );
                  FFLog.result(serverStartSuccess, "打开蓝牙服务端");
                },
              ),
              FFTestClick(
                text: "服务端广播消息",
                onClick: () async {
                  await bluePlugin.bluetoothServerBroadcastMsg(
                      msg: (msgIndex++).toString());
                },
              ),
              FFTestClick(
                text: "关闭蓝牙服务端",
                onClick: () async {
                  await bluePlugin.bluetoothStopServer();
                },
              ),
              FFTestClick(
                text: "连接设备",
                onClick: () async {
                  bool startSuccess = await bluePlugin.bluetoothStartDiscovery(
                    name: "LedServer",
                    uuid: "E20A39F4-73F5-4BC4-A12F-17D1AD07A961",
                  );
                  FFLog.result(startSuccess, "扫描开启状态");
                },
              ),
              FFTestClick(
                text: "结束扫描",
                onClick: () async {
                  await bluePlugin.bluetoothEndDiscovery();
                },
              ),
              FFTestClick(
                text: "客户端断开连接",
                onClick: () async {
                  await bluePlugin.bluetoothStopClient(
                      uuid: "E20A39F4-73F5-4BC4-A12F-17D1AD07A961",
                      device: "LedServer");
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}
